package com.cy.pj.goods.controller;

import com.cy.pj.goods.pojo.Goods;
import com.cy.pj.goods.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Controller
@RequestMapping("/goods/")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @GetMapping("doFindGoods")
    public String doFindGoods(Model model) {
        List<Goods> list = goodsService.findGoods();
        model.addAttribute("list", list);
        return "goods";
    }

    @RequestMapping("doDeleteById")
    public String doDeleteById(Integer id,Model model){
        goodsService.deleteById(id);
        List<Goods> list=goodsService.findGoods();
        model.addAttribute("list", list);
        return "goods";
    }

    @RequestMapping("/turn")
    public String doGoodsturn(){
        System.out.println("rua");
        return "goods";
    }

    @GetMapping("doGoodsAddUI")
    public String doGoodsAddUI(){
        return "goods-add";
    }

    @GetMapping("doFindById/{id}")
    public String doFindById(@PathVariable Integer id, Model model){
        Goods goods = goodsService.findById(id);
        model.addAttribute("goods", goods);
        return "goods-update";
    }

    @PostMapping("doUpdateGoods")
    public String doUpdateGoods(Goods goods,Model model){
        goodsService.updateGoods(goods);
        List<Goods> list = goodsService.findGoods();
        model.addAttribute("list", list);
        return "goods";
    }

    @PostMapping("doSaveGoods")
    public String doSaveGoods(Goods goods,Model model){
        goodsService.saveGoods(goods);
        List<Goods> list = goodsService.findGoods();
        model.addAttribute("list", list);
        return "goods";
    }

    @RequestMapping("doFindByName")
    public String doFindByName(String name, Model model){
        List<Goods> list = goodsService.findByName(name);

        for (Goods g:list
             ) {
            System.out.println(g);
        }
        model.addAttribute("list", list);
        return "goods";
    }



    /*@ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public String doHandleRuntimeException(RuntimeException e){
        log.error("GoodsController.exception{}",e.getMessage());
        return e.getMessage();
    }*/
}
/*

@Controller
@RequestMapping("/goods/")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;


    @GetMapping("/doFindGoods")
    public String doFindGoods(Model model) {
        List<Goods> list = goodsService.findGoods();
        model.addAttribute("list", list);
        return "goods";
    }

    @RequestMapping("/doFindById")
    public String doFindById(Integer id,Model model) {
        Goods goods = goodsService.findGoodsById(id);
        model.addAttribute("goods",goods);
        System.out.println(goods);
        return "goods_update";
    }

    @RequestMapping("/turn")
    public String turn() {
        return "goods_insert";
    }

    @RequestMapping("/doInsert")
    public String doInsert(Goods goods) {
        goodsService.insert(goods);
        return "forward:/goods/doFindGoods";
    }

    @RequestMapping("/doUpdate")
    public String doUpdate(Goods goods) {
        goodsService.update(goods);
        return "forward:/goods/doFindGoods";
    }

    @RequestMapping("/doDelete")
    //@RequestBody
    public String doDelete(@RequestParam("id") Integer id) {
        goodsService.delete(id);
        return "forward:/goods/doFindGoods";
    }
*/
